﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Knihotoc_WP8.Data;
using System.Windows.Media.Imaging;
using System.Windows.Input;

namespace Knihotoc_WP8.CustomControls
{
    public partial class LoggedInUserData : UserControl
    {
        public LoggedInUserData()
        {
            this.InitializeComponent();
        }

        private User loggedUser;

        public LoggedInUserData(User loggedUser)
            : this()
        {
            this.loggedUser = loggedUser;
            reloadValues();
        }

        public void reloadValues()
        {
            List<Book> booksSource = new List<Book>();
            foreach (Book book in loggedUser.BorrowedBooks)
            {
                booksSource.Add(book);
            }
            try
            {
                BitmapImage imageOnline = new BitmapImage(new Uri(loggedUser.ImageLink));
                imageOnline.ImageFailed += new EventHandler<ExceptionRoutedEventArgs>(ImageOnline_ImageFailed);
                this.userImage.Source = imageOnline;
            }
            catch (Exception e)
            {
                int a = 10;
            }

            this.usernameText.Text = String.Format("{0} {1}", loggedUser.FirstName, loggedUser.SurName);
            this.borrowedBooksField.Text = loggedUser.TotalBorrowed + "";
            this.foundBooksField.Text = loggedUser.TotalFound + "";

            this.currentlyReadingListBox.ItemsSource = booksSource;
        }

        private void ImageOnline_ImageFailed(object sender, ExceptionRoutedEventArgs e)
        {
        }

        public event EventHandler ShowAllMyBooks;

        private void BookItemTemplate_Tap(object sender, GestureEventArgs e)
        {
        }

        private void AllMyBooksButton_Click(object sender, RoutedEventArgs e)
        {
            if (this.ShowAllMyBooks != null)
            {
                this.ShowAllMyBooks.Invoke(this, new EventArgs());
            }
        }
    }
}
